//
//  TestSocketController.m
//  BSProject
//
//  Created by nsm on 2019/8/1.
//  Copyright © 2019 beisheng. All rights reserved.
//

#import "TestSocketController.h"
#import "FDWebSocket.h"
#import "BSSocket.h"

@interface TestSocketController ()
@property (nonatomic, strong) NSMTextView *tv;
@end

@implementation TestSocketController

#pragma mark - lifeCycle

#pragma mark - override

#pragma mark - initialization
- (void)viewDidLoad {
    [super viewDidLoad];
    
    MyLinearLayout *layout = [MyLinearLayout linearLayoutWithOrientation:MyOrientation_Vert];
    layout.myHorzMargin = 0;
    layout.gravity = MyGravity_Horz_Center;
    layout.padding = UIEdgeInsetsMake(20, 20, 20, 20);
    layout.subviewVSpace = 20;
    [self.view addSubview:layout];
    
    {
        MyLinearLayout *sub = [MyLinearLayout linearLayoutWithOrientation:MyOrientation_Horz];
        sub.myHorzMargin = 0;
        sub.wrapContentHeight = YES;
        sub.subviewHSpace = 20;
        sub.gravity = MyGravity_Vert_Center;
        [layout addSubview:sub];
        
        {
            NSMButton *btn = NSMButton.new;
            btn.weight = 1;
            btn.height = 44;
            btn.backgroundColor = RandomColor();
            btn.titleLabel.font = CustomFont(14);
            [btn setTitleColor:COLOR_4 forState:UIControlStateNormal];
            btn.title = @"连接";
            [sub addSubview:btn];
            [btn addTarget:self action:@selector(webSocketConnent) forControlEvents:UIControlEventTouchUpInside];
        }
        {
            NSMButton *btn = NSMButton.new;
            btn.weight = 1;
            btn.height = 44;
            btn.backgroundColor = RandomColor();
            btn.titleLabel.font = CustomFont(14);
            [btn setTitleColor:COLOR_4 forState:UIControlStateNormal];
            btn.title = @"断开";
            [sub addSubview:btn];
            [btn addBlockForControlEvents:UIControlEventTouchUpInside block:^(id  _Nonnull sender) {
                [_FDWebSockect close];
            }];
        }
    }
    
    {
        MyLinearLayout *sub = [MyLinearLayout linearLayoutWithOrientation:MyOrientation_Horz];
        sub.myHorzMargin = 0;
        sub.wrapContentHeight = YES;
        sub.subviewHSpace = 20;
        sub.gravity = MyGravity_Vert_Center;
        [layout addSubview:sub];
        
        UITextField *field = UITextField.new;
        field.weight = 1;
        field.height = 36;
        field.backgroundColor = UIColor.yellowColor;
        field.textColor = COLOR_0;
        field.clearButtonMode = UITextFieldViewModeWhileEditing;
        field.text = @"2V";
        [sub addSubview:field];
        
        NSMButton *btn = NSMButton.new;
        btn.size = CGSizeMake(100, 44);
        btn.backgroundColor = RandomColor();
        btn.titleLabel.font = CustomFont(14);
        [btn setTitleColor:COLOR_4 forState:UIControlStateNormal];
        btn.title = @"发送";
        [sub addSubview:btn];
        
        [btn addBlockForControlEvents:UIControlEventTouchUpInside block:^(id  _Nonnull sender) {
            // 结构体命令码自行替换
            [_FDWebSockect send:@{} main:0 sub:0];
        }];
    }
    
    NSMTextView *textView = NSMTextView.new;
    textView.backgroundColor = UIColor.redColor;
    textView.size = CGSizeMake(200, 300);
    textView.textColor = COLOR_0;
    [layout addSubview:textView];
    _tv = textView;
}

#pragma mark - setter/getter

#pragma mark - network

#pragma mark - method/public

#pragma mark - method/private
- (void)showText:(id)text {
    dispatch_async(dispatch_get_main_queue(), ^{
        NSLog(@"%@", text);
        self.tv.text = FormatString(@"%@\n%@", self.tv.text, text);
    });
}

#pragma mark - action
- (void)webSocketConnent {
    // 如果登录了开启长链接,里面有个URL自行替换
    [_FDWebSockect connect:@{}];
#ifdef DEBUG
    // 调试模式开启控制台打印
    @nsm_weak(self)
    _FDWebSockect.didReceivePong = ^(NSData * _Nonnull pongPayload) {
        @nsm_strong(self)
        [self showText:@"收到心跳"];
    };
    _FDWebSockect.didConnectFailure = ^(NSError * _Nonnull error) {
        @nsm_strong(self)
        [self showText:@"连接失败"];
    };
    _FDWebSockect.didClose = ^(NSInteger code, NSString * _Nonnull reason, BOOL wasClean) {
        @nsm_strong(self)
        [self showText:FormatString(@"连接关闭:%@", reason)];
    };
    _FDWebSockect.didSendData = ^(id  _Nonnull data) {
        @nsm_strong(self)
        [self showText:FormatString(@"发送加密数据:\n%@", @"")];
    };
    _FDWebSockect.didReceiveDataBefore = ^(id  _Nonnull data) {
        @nsm_strong(self)
        
        [self showText:FormatString(@"收到解密前数据:%@", data)];

    };
    _FDWebSockect.didReceiveData = ^(NSDictionary * _Nonnull dataDict) {
        @nsm_strong(self)
        [self showText:FormatString(@"收到解密数据:%@", dataDict.printString)];
    };
    _FDWebSockect.didReconnecting = ^(NSUInteger count) {
        @nsm_strong(self)
        [self showText:FormatString(@"第%@次重连...%@", @(count), @([NSThread.currentThread isMainThread]))];
    };
#endif
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

@end
